﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Mabalacat.CPS.DataAccess.DataObjects;


namespace Mabalacat.CPS.DataAccess.Context
{
    public class PayrollDataContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Signatory> Signatories { get; set; }
        public DbSet<Division> Divisions { get; set; }
        public DbSet<Position> Positions { get; set; }
        public DbSet<PhilHealthTable> PhilHealthTable { get; set; }             
        
        public PayrollDataContext(string connectionString)
            : base(connectionString)
        {

        }        
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            

            modelBuilder.Entity<User>()
              .HasMany(u => u.Roles)
              .WithMany(r => r.Users)
              .Map( m =>
               {
                    m.MapLeftKey("UserId");
                    m.MapRightKey("RoleId");
                    m.ToTable("UserRoles");
               });

            modelBuilder.Entity<Position>()
                .HasMany(p => p.Employees)
                .WithOptional(e => e.Position)
                .HasForeignKey(e => e.PositionId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Division>()
                .HasMany(d => d.Employees)
                .WithOptional(e => e.Division)
                .HasForeignKey(e => e.DivisionId)
                .WillCascadeOnDelete(false);

        }
    }
}
